package itbour.onetouchshow.activity.video.videoplay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsjr.callback.EncryBeanCallBack;
import com.lsjr.utils.HttpUtils;

import java.util.HashMap;

import itbour.onetouchshow.App;
import itbour.onetouchshow.base.mvp.BasePresenterImpl;
import itbour.onetouchshow.bean.ModelVideoBean;
import itbour.onetouchshow.constants.AppConfig;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.utils.DeviceUtil;
import itbour.onetouchshow.utils.L_;
import itbour.onetouchshow.utils.T_;
import itbour.onetouchshow.utils.UIUtils;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class VideoplayPresenter extends BasePresenterImpl<VideoplayContract.View> implements VideoplayContract.Presenter {


    /**
     * 获取Vip数据
     */
    public void getVipPageInfo() {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("device", AppConst.ANDROID_DEVICE);
        String appVersionName = DeviceUtil.getAppVersionName(UIUtils.getContext());
        stringObjectHashMap.put("appVer", appVersionName);
        HttpUtils.getInstance().executeGet(AppConfig.GET_VIP_PAGE_INFO, stringObjectHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                L_.e(exception);
                T_.showCustomToast(exception);
                mvpView.loadVipInfoFail(exception);
            }

            @Override
            protected void onSuccess(String response) {
                mvpView.loadVipInfoSuccess(response);
            }
        });
    }


    public void goToMack(ModelVideoBean videoBean, int tag) {
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        L_.e(videoBean.getId() + "");
        stringStringHashMap.put("device", AppConst.ANDROID_DEVICE);
        stringStringHashMap.put("id", videoBean.getId());
        stringStringHashMap.put("userId", App.getUserId());
        stringStringHashMap.put("isTmpl", tag);
        HttpUtils.getInstance().executeGet(AppConfig.GET_DOC_CONTENT_V10, stringStringHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                L_.e(exception);
                T_.showCustomToast(exception);
            }

            @Override
            protected void onSuccess(String response) {

                if (mvpView != null) {
                    mvpView.loadMoudelSuccess(response);
                }

            }
        });

    }

    /**
     * 获取当前是否收藏
     *
     * @param data
     */
    public void loadThisIsCollect(ModelVideoBean data) {
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("device", 610);
        stringStringHashMap.put("tmplId", data.getId());
        stringStringHashMap.put("userId", App.getUserId());
        HttpUtils.getInstance().executeGet(AppConfig.GET_THIS_MODEL_ISCOLLECT_V10, stringStringHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                if (mvpView != null) {
                    mvpView.loadFaild(exception);
                }
            }

            @Override
            protected void onSuccess(String response) {
                L_.i("GET_VERTICAL_MODEL_PREVIEW_V10===" + response + "id===" + data.getId());
                if (mvpView != null) {
                    JSONObject jsonObject = JSON.parseObject(response);
                    int code = jsonObject.getIntValue(AppConfig.ISCOLLECT);
                    if (code == 0) {
                        mvpView.unCollect();
                    } else {
                        mvpView.isCollect();
                    }

                }
            }
        });
    }


    public void collectOrCancalCollectThisModel(boolean collect, int id) {
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("device", 610);
        stringStringHashMap.put("tmplId", id);
        stringStringHashMap.put("userId", App.getUserId());
        int collectTag;
        if (collect) {
            collectTag = 0;
        } else {
            collectTag = 1;
        }
        stringStringHashMap.put("collect", collectTag);
        final int finalCollectTag = collectTag;
        HttpUtils.getInstance().executeGet(AppConfig.SET_CANCEL_MODEL_COLLECT_V10, stringStringHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                if (mvpView != null) {
                    mvpView.collectFaild();

                }
            }

            @Override
            protected void onSuccess(String response) {
                L_.i("SET_CANCEL_MODEL_COLLECT_V10===" + response + "id===" + id);
                if (mvpView != null) {
                    if (finalCollectTag == 0) {
                        mvpView.unCollect();
                        mvpView.collectSuccess("取消收藏成功");
                        T_.showCustomToast("取消收藏");
                    } else {
                        mvpView.isCollect();
                        mvpView.collectSuccess("收藏成功");
                        T_.showCustomToast("收藏成功");
                    }
                }
            }
        });
    }


    /**
     * 获取模板预览
     */
    public void loadThisPageInfo(String id) {
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("device", 610);
        stringStringHashMap.put("id", id);
        HttpUtils.getInstance().executeGet(AppConfig.GET_VERTICAL_MODEL_PREVIEW_V10, stringStringHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                if (mvpView != null) {
                    // T_.showToastReal(exception);
                    mvpView.loadFaild(exception);
                    //  mvpView.loadPageInfoFail(exception);
                }
            }

            @Override
            protected void onSuccess(String response) {
                L_.i("response===" + response);
                if (mvpView != null) {
                    mvpView.loadPageInfoSuccess(response);
                }
            }
        });
    }


    /**
     * 获取作品预览
     */
    public void loadProductPreviewData(String docId) {
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("device", AppConst.ANDROID_DEVICE);
        stringStringHashMap.put("userId", App.getUserId());
        stringStringHashMap.put("docId", docId);

        HttpUtils.getInstance().executeGet(AppConfig.GET_DOC_PREVIEW_V10, stringStringHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                if (mvpView != null) {
                    L_.i(exception);
                    mvpView.loadFaild(exception);
                }
            }

            @Override
            protected void onSuccess(String response) {
                L_.e("loadProductPreviewData" + response);
                if (mvpView != null) {
                    mvpView.loadProductPreviewData(response);
                }
            }
        });

    }

    /**
     * 作品预览  new
     */
    public void newLoadDocPreview(String docId) {
        L_.e("作品预览" + docId);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("device", AppConst.ANDROID_DEVICE);
        stringObjectHashMap.put("userId", App.getUserId());
        stringObjectHashMap.put("docId", docId);
        HttpUtils.getInstance().executePost(AppConfig.REQUESTUSERDOCPRIVEWINFO_WEB_V1_1, stringObjectHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                T_.showCustomToast(exception);
                L_.e("newLoadDocPreview" + exception);
                if (mvpView != null) {
                    mvpView.loadFaild(exception);
                }
            }

            @Override
            protected void onSuccess(String response) {
                if (mvpView != null) {
                    L_.e("newLoadDocPreview" + response);
                    mvpView.loadNewDocPreviewSuccess(response);
                }
            }
        });
    }


    public void loadUserDetailInfo() {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("device", AppConst.ANDROID_DEVICE);
        stringObjectHashMap.put("userId", App.getUserId());
        HttpUtils.getInstance().executeGet(AppConfig.GETUSERDETAILINFO_V1_0, stringObjectHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                L_.e(exception);
                T_.showCustomToast(exception);
            }

            @Override
            protected void onSuccess(String response) {
                // SpUtils.getInstance().saveString(AppConst.USER_INFO, response);
                mvpView.loaduserDetailSuccess(response);
            }
        });
    }

}
